package cn.ygh.bbo.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.ygh.bbo.framerwork.core.Grid;
import cn.ygh.bbo.common.exception.TipException;
import cn.ygh.bbo.common.annotation.DataScope;
import cn.ygh.bbo.user.bean.UserOpretationLog;
import cn.ygh.bbo.user.mapper.UserOpretationLogMapper;
import cn.ygh.bbo.user.service.UserOpretationLogService;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 描述：操作日志  ServiceImpl
 * @author ygh
 */
@Service
public class UserOpretationLogServiceImpl implements UserOpretationLogService{

    private final Logger logger=LoggerFactory.getLogger(getClass());

    @Autowired
    UserOpretationLogMapper mapper;

    /**
    * 保存对象
    * @param  userOpretationLog 对象信息
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int save(UserOpretationLog userOpretationLog) {
        //保存之前通用处理
        beforeCreate(userOpretationLog);
        userOpretationLog.def();
        return mapper.insert(userOpretationLog);
    }

    /**
    * 更新对象
    * @param  userOpretationLog 对象信息
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(UserOpretationLog userOpretationLog) {
        if(userOpretationLog.getId()==null){
            throw new TipException("id can not null");
        }
        //更新之前通用处理
        beforeUpdate(userOpretationLog);
        userOpretationLog.def();
        return mapper.updateById(userOpretationLog);
    }

    /**
    * 批量更新
    * @param  userOpretationLog 对象信息
    * @param ids
    * @return
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBatchByIds(UserOpretationLog userOpretationLog, Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        return mapper.updateBatchByIds(userOpretationLog,ids);
    }

    /**
    * 根据ID删除对象
    * @param id
    * @return
    */
    @Override
    public int deleteById(Serializable id) {
        if(id==null){
            throw new TipException("id can not null");
        }
        if(checkLink(id)){
            return mapper.deleteById(id);
        }
        return 0;
    }

    /**
    * 删除关联判断
    * @param id
    */
    private boolean checkLink(Serializable id) {
        return true;
    }

    /**
    * 根据ID数组批量删除对象
    * @param ids
    * @return
    */
    @Override
    public int deleteByIds(Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        int result=0;
        for (Serializable id:ids){
            result+=deleteById(id);
        }
        return result;
    }

    /**
    * 根据ID获取对象
    * @param id
    * @return
    */
    @Override
    public UserOpretationLog getById(Serializable id) {
        if(id==null){
            throw new TipException("id can not null");
        }
        return mapper.selectById(id);
    }

    /**
    * 根据ID数组获取对象列表
    * @param ids
    * @return
    */
    @Override
    public List<UserOpretationLog> listfindByIn(Serializable[] ids) {
        if(ids==null || ids.length==0){
            throw new TipException("ids can not null");
        }
        return mapper.selectBatchIds(Arrays.asList(ids));
    }

    /**
    * 根据对象信息获取满足条件的对象列表
    * @param  userOpretationLog 对象信息
    * @param pageable 分页条件为空则不分页
    * @return
    */
    @DataScope
    @Override
    public List<UserOpretationLog> listfind(UserOpretationLog userOpretationLog, Pageable pageable) {
        if (pageable!=null && pageable.getPageNumber()!=0){
            PageHelper.startPage(pageable.getPageNumber()
                    , pageable.getPageSize(), false);
        }
        return mapper.selectByObj(userOpretationLog);
    }

    /**
    * 根据对象信息获取满足条件的对象列表和总数
    * @param  userOpretationLog 对象信息
    * @param pageable 分页条件
    * @return
    */
    @DataScope
    @Override
    public Grid listGrid(UserOpretationLog userOpretationLog, Pageable pageable) {
        PageHelper.startPage(pageable.getPageNumber()
                , pageable.getPageSize(), true);
        List<UserOpretationLog> userOpretationLogs = mapper.selectByObj(userOpretationLog);
        PageInfo<UserOpretationLog>pageInfo=new PageInfo<>(userOpretationLogs);
        Grid grid=new Grid();
        grid.setTotal((int) pageInfo.getTotal());
        grid.setRows(pageInfo.getList());
        return grid;
    }

}
